package com.qymerge.manage.base.service.impl;

import com.qymerge.manage.base.dao.IMetaProfitReportItemDao;
import com.qymerge.manage.base.dao.IMetaProfitTableTemplateDao;
import com.qymerge.manage.base.ro.MetaProfitReportItemRO;
import com.qymerge.manage.base.ro.MetaProfitReportItemRO;
import com.qymerge.manage.base.service.IMetaProfitReportItemService;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>Title: MetaProfitReportItemServiceImpl</p>
 * <p>Description: 利润表报表项目配置</p>
 *
 * @author Xiao
 * @date 2025/4/29 18:43
 * @version 1.0.0
 */
@Service
public class MetaProfitReportItemServiceImpl implements IMetaProfitReportItemService {

    @Autowired
    private IMetaProfitReportItemDao metaProfitReportItemDao;
    @Autowired
    private IMetaProfitTableTemplateDao metaProfitTableTemplateDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(MetaProfitReportItemRO ro, PageRequest pageRequest) {
        List<MetaProfitReportItemRO> list = metaProfitReportItemDao.list(ro, pageRequest);
        for (MetaProfitReportItemRO metaProfitReportItemRO : list) {
            if (metaProfitReportItemRO.getTemplateId() > 0) {
                metaProfitReportItemRO.setTemplateInfo(metaProfitTableTemplateDao.findOne(metaProfitReportItemRO.getTemplateId()));
            }
            if (metaProfitReportItemRO.getParentId() > 0) {
                metaProfitReportItemRO.setParentInfo(metaProfitReportItemDao.findOne(metaProfitReportItemRO.getParentId()));
            }
        }
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = metaProfitReportItemDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    @Override
    public List<MetaProfitReportItemRO> treeList(MetaProfitReportItemRO roSearch) {
        List<MetaProfitReportItemRO> reportItemDaoAll = metaProfitReportItemDao.findAll(roSearch);
        for (MetaProfitReportItemRO metaProfitReportItemRO : reportItemDaoAll) {
            if (metaProfitReportItemRO.getTemplateId() > 0) {
                metaProfitReportItemRO.setTemplateInfo(metaProfitTableTemplateDao.findOne(metaProfitReportItemRO.getTemplateId()));
            }
            if (metaProfitReportItemRO.getParentId() > 0) {
                metaProfitReportItemRO.setParentInfo(metaProfitReportItemDao.findOne(metaProfitReportItemRO.getParentId()));
            }
        }
        return reportItemDaoAll;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MetaProfitReportItemRO detail(Long id) {
        MetaProfitReportItemRO profitReportItemDaoOne = metaProfitReportItemDao.findOne(id);
        if (profitReportItemDaoOne.getTemplateId() > 0) {
            profitReportItemDaoOne.setTemplateInfo(metaProfitTableTemplateDao.findOne(profitReportItemDaoOne.getTemplateId()));
        }
        if (profitReportItemDaoOne.getParentId() > 0) {
            profitReportItemDaoOne.setParentInfo(metaProfitReportItemDao.findOne(profitReportItemDaoOne.getParentId()));
        }
        return profitReportItemDaoOne;
    }

    /**
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(MetaProfitReportItemRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = metaProfitReportItemDao.insert(ro);
        } else {
            rn = metaProfitReportItemDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    @Override
    public ActionResultRO delete(Long id) {
        String msg = "";
        String errMsg = "";
        int rn = 0;
        
        // 二级查询
        List<MetaProfitReportItemRO> secondAll = metaProfitReportItemDao.findAllByParentId(id);
        if (CollectionUtils.isNotEmpty(secondAll)) {
            for (MetaProfitReportItemRO second : secondAll) {
                long secondParentId = second.getId();

                // 三级
                List<MetaProfitReportItemRO> thirdAll = metaProfitReportItemDao.findAllByParentId(secondParentId);
                if (CollectionUtils.isNotEmpty(thirdAll)) {
                    for (MetaProfitReportItemRO third : thirdAll) {
                        Long thirdParent = third.getId();

                        // 四级
                        List<MetaProfitReportItemRO> fourAll = metaProfitReportItemDao.findAllByParentId(thirdParent);
                        if (CollectionUtils.isNotEmpty(fourAll)) {
                            for (MetaProfitReportItemRO four : fourAll) {
                                long fourParentId = four.getId();

                                metaProfitReportItemDao.remove(fourParentId);
                            }
                        }
                        metaProfitReportItemDao.remove(thirdParent);
                    }
                }
                metaProfitReportItemDao.remove(secondParentId);
            }
        }
        // 删除本菜单
        rn = metaProfitReportItemDao.remove(id);
        msg = "删除成功";
        errMsg = "删除失败";
        return new ActionResultRO(true, msg, errMsg);
    }

}
